import { ref } from "vue";
import type { tutorialData } from "@/type/resource.ts";
import { useBodyStore } from "@/store/body.ts";
import { ElMessage } from "element-plus";
import { getTutorialAPI, deleteTutorialAPI, getTutorialBodyAPI } from "@/api/resource.ts";

export default function useTutorial() {

  const bodyStore = useBodyStore()
  const dialog = ref()
  const searchBody = ref('')
  const tutorialList = ref<tutorialData[]>()

  // 获取教程列表
  const getTutorial = async () => {
    const res = await getTutorialAPI()
    tutorialList.value = res.data
  }

  // 搜索部位教程
  const onSearchBody = async () => {
    const res = await getTutorialBodyAPI('', searchBody.value)
    tutorialList.value = res.data
  }

  // 重置
  const restList = async () => {
    searchBody.value = ''
    await getTutorial()
  }

  // 添加部位教程
  const addTutorial = async () => {
    dialog.value.init({}, 1)
  }

  // 编辑课程
  const EditTutorial = (row: any,) => {
    dialog.value.init(row, 2)
  }

  // 删除部位教程
  const deleteTutorial = async (id: string) => {
    const res: any = await deleteTutorialAPI(id)
    if (res.code === 0) {
      await getTutorial()
      ElMessage({
        type: 'success',
        message: res.message
      })
    }
  }

  return {
    dialog,
    bodyStore,
    tutorialList,
    searchBody,
    addTutorial,
    restList,
    onSearchBody,
    EditTutorial,
    deleteTutorial,
    getTutorial
  }
}
